#include <reg52.h>
#include <stdio.h>                       /* standard I/O .h-file              */
#include <stdlib.h>                      /* standard library .h-file          */
#include <ctype.h>                       /* character functions               */

//#ifdef MONITOR51                         /* Debugging with Monitor-51 needs   */
//char code reserve [3] _at_ 0x23;         /* space for serial interrupt if     */
//#endif                                   /* Stop Exection with Serial Intr.   */
	                                     /* is enabled                        */
extern void getline (unsigned char idata *line, unsigned char n); /*input line*/

//char  ERROR [] = "\n*** ERROR: %s\n";   /* ERROR message string in code   */
//char  BufSend[] = {0x40,0x30,0x30,0x30,0x31,0x36,0x34,0x30,0x33,0x0d};
//unsigned char  BufSend[] = {0x40,0x30,0x30,0x30,0x31,0x30,0x30,0x39,0x39,0x0d};
unsigned char  BufSend[] = {0x40,0x30,0x30,0x30,0x31,0x30,0x30,0x30,0x30,0x39,0x39,0x0d};
 
unsigned char BufASCII[2];

unsigned char BufAxisData[3];

void InitSerialPort(void)
{
    SCON  = 0x50;				// SCON: mode 1, 8-bit UART, enable rcvr      		
    TMOD |= 0x20;               // TMOD: timer 1, mode 2, 8-bit reload        		
    TH1   = 253;                // TH1:  reload value for 9600 baud @ 11.0592MHz   	
//    TH1   = 250;                // TH1:  reload value for 9600 baud @ 22.1184MHz   	
    TR1   = 1;                  // TR1:  timer 1 run                          		
    TI    = 1;                  // TI:   set TI to send first char of UART    		
}

/*
void Delay100ms(void)
{
	unsigned int i,j;
	for(i=0;i<200;i++)
		for(j=0;j<100;j++){};
}
*/

/*
void Delay2s(void)
{
			unsigned int i;
	for(i=0;i<20;i++)
    {
		Delay100ms();
    }		
}	
*/
unsigned char fnHexToASCII(unsigned char HexVal)
{
	if((HexVal & 0x0f) >= 0x0a)
	{
		return(HexVal + 0x37);
	}
	else
	{
		return(HexVal + 0x30);
	}
}

void fnBCDToASCII(unsigned char BCDVal,unsigned char *buf)
{
	unsigned char temp;

	temp = BCDVal & 0x0f;
	buf[1] = fnHexToASCII(temp);
	BCDVal >>= 4;
	temp = BCDVal & 0x0f;
	buf[0] = fnHexToASCII(temp);
}

void fnGetXorVal(unsigned char *buf)
{
	unsigned char i,ResultXor;

	ResultXor = 0;
//	for(i=1;i<7;i++)		//byte 
	for(i=1;i<9;i++)		//word
	{
//		ResultXor ^= BufSend[i]; 
		ResultXor ^= buf[i]; 
	}
	
//	fnBCDToASCII(ResultXor,&BufSend[7]);
	fnBCDToASCII(ResultXor,&buf[9]);

}
/******************************************************************************/
/***************************      MAIN PROGRAM      ***************************/
/******************************************************************************/
void main ( void )  {                          /* main entry for program      */
//	unsigned char idata cmdbuf [15];             /* command input buffer        */
	unsigned char idata cmdbuf [20];             /* command input buffer        */
	unsigned char i,j;                             /* index for command buffer    */
	unsigned char index;

	InitSerialPort();
	EA = 1;                                     /* global interrupt enable     */

//	printf ( menu );                             /* display command menu        */
	printf("AT89C52 Start...\n");

/*
	while(1)
	{
//		j = P0;
//		putchar(fnHexToASCII(P0 & 0x0f));
//		putchar(0x0d);

//		printf("LVal=%c,HVal=%c\n",BufASCII[0],BufASCII[1]);
//		printf("XorHVal=%c,XorLVal=%c\n",BufSend[7],BufSend[8]);
		for(i=0;i<10;i++)
		{
			printf("%02bx ",BufSend[i]);
		}
		printf("\n");

//		printf("SW Val = %02x\n",j );
		for(i=0;i<20;i++)
		{
			Delay100ms();
		}
	}
*/
	while (1)  
	{                                 				/* loop forever                */
		BufAxisData[0] = P0;
		BufAxisData[1] = P1;
		BufAxisData[2] = P2;

//		printf ("\nCommand: ");                    
   		getline (&cmdbuf[0], sizeof (cmdbuf));     /* input command line          */

//	    for (i = 0; cmdbuf[i] != 0; i++)  {        /* convert to upper characters */
//      	cmdbuf[i] = toupper(cmdbuf[i]);
//    	}

//    	for (i = 0; cmdbuf[i] == ' '; i++);        /* skip blanks                 */

		switch (cmdbuf[i])  
		{                      /* proceed to command function */

			case 0x40:
			{
/*
				switch (cmdbuf[8])
				{
					case 0x30:
						index = 0;
						break;
					case 0x31:
						index = 1;
						break;
					case 0x32:
						index = 2;
						break;
					default:
						index = 2;
						break;
				}
*/
//				fnBCDToASCII(BufAxisData[index],&BufSend[5]);
				fnBCDToASCII(BufAxisData[0],&BufSend[7]);
				fnGetXorVal(BufSend);
//				for(j=0;j<10;j++)		//byte
				for(j=0;j<12;j++)		//word
				{
					putchar(BufSend[j]);
				}
			}
			break;

      default:                                 /* Error Handling              */
        break;
    }
  }
}

